package nongchang.kuaijiecaifu.com.ncapp_android.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import nongchang.kuaijiecaifu.com.ncapp_android.R;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.AllCart;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.HomeItemCart;

/**
 * Created by Administrator on 2016/6/14 0014.
 */
public class OrderExpandAdapter extends BaseExpandableListAdapter {
    private List<AllCart> groupList;
    private Context context;

    public OrderExpandAdapter(List<AllCart> groupList, Context context) {
        this.groupList = groupList;
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (groupList.get(groupPosition).itemList != null) {
            return groupList.get(groupPosition).itemList.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return groupList.get(groupPosition).itemList.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder groupHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_all_order, null);
            groupHolder = new GroupHolder();
            groupHolder.ll_multiple = (LinearLayout) convertView.findViewById(R.id.ll_multiple);
            groupHolder.rl_multiple = (RelativeLayout) convertView.findViewById(R.id.rl_multiple);
            groupHolder.rl_simple = (RelativeLayout) convertView.findViewById(R.id.rl_simple);
            groupHolder.txt_simple_money = (TextView) convertView.findViewById(R.id.txt_simple_money);
            groupHolder.txt_simple_num = (TextView) convertView.findViewById(R.id.txt_simple_num);
            groupHolder.txt_simple_name = (TextView) convertView.findViewById(R.id.txt_simple_name);

            groupHolder.img_cb_simple_img_good = (ImageView) convertView.findViewById(R.id.img_cb_simple_img_good);
            groupHolder.txt_money = (TextView) convertView.findViewById(R.id.txt_money);
            groupHolder.layout_message = (LinearLayout) convertView.findViewById(R.id.layout_message);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) convertView.getTag();
        }
        AllCart item = groupList.get(groupPosition);
        if (item.type == 1) {
            groupHolder.rl_multiple.setVisibility(View.VISIBLE);
            groupHolder.rl_simple.setVisibility(View.GONE);
            int total = 0;
            for (int i = 0, size = item.itemList.size(); i < size; i++) {
                if (item.itemList.get(i).isSelect) {
                    total += item.itemList.get(i).total_money;
                }
            }
            groupHolder.txt_money.setText("￥" + total);

        }
        if (item.type == 2) {
            groupHolder.rl_multiple.setVisibility(View.GONE);
            groupHolder.rl_simple.setVisibility(View.VISIBLE);
            groupHolder.txt_simple_money.setText("￥" + item.money);
            groupHolder.txt_simple_num.setText("×" + item.number + "");
            groupHolder.txt_simple_name.setText(item.title);
            Glide.with(context).load(item.image).into(groupHolder.img_cb_simple_img_good);

        }
        if (!isExpanded) {
            if (item.type == 1)
                groupHolder.ll_multiple.setVisibility(View.VISIBLE);
            groupHolder.rl_multiple.setBackgroundColor(Color.WHITE);
            groupHolder.layout_message.setVisibility(View.VISIBLE);
//            换图标
        } else {
            if (item.type == 1)
                groupHolder.ll_multiple.setVisibility(View.GONE);
            groupHolder.layout_message.setVisibility(View.GONE);
            groupHolder.rl_multiple.setBackgroundColor(0xFFF8F8F8);
        }
        //设置数据
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ItemHolder itemHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_order_copy_simple_cart, null);
            itemHolder = new ItemHolder();
            itemHolder.txt_simple_money = (TextView) convertView.findViewById(R.id.txt_simple_money);
            itemHolder.txt_simple_num = (TextView) convertView.findViewById(R.id.txt_simple_num);
            itemHolder.img_cb_simple_img_good = (ImageView) convertView.findViewById(R.id.img_cb_simple_img_good);
            itemHolder.rl_simple = (RelativeLayout) convertView.findViewById(R.id.rl_simple);
            itemHolder.layout_simple_message = (LinearLayout) convertView.findViewById(R.id.layout_simple_message);
            convertView.setTag(itemHolder);
        } else {
            itemHolder = (ItemHolder) convertView.getTag();
        }
        HomeItemCart item = groupList.get(groupPosition).itemList.get(childPosition);
        if (item.isSelect) {

            itemHolder.txt_simple_money.setText("￥" + item.total_money);
            itemHolder.txt_simple_num.setText("×" + item.num + "");
            Glide.with(context).load(item.image).into(itemHolder.img_cb_simple_img_good);
            if (isLastChild) {
                itemHolder.layout_simple_message.setVisibility(View.VISIBLE);
            }
        } else {
            itemHolder.txt_simple_money.setText("");
            itemHolder.txt_simple_num.setText("×0");
            Glide.with(context).load(item.image).into(itemHolder.img_cb_simple_img_good);

        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class GroupHolder {
        LinearLayout ll_multiple;
        RelativeLayout rl_multiple;
        RelativeLayout rl_simple;
        TextView txt_money;
        TextView txt_simple_name;
        ImageView img_cb_simple_img_good;
        TextView txt_simple_money;
        TextView txt_simple_num;
        LinearLayout layout_message;
    }

    class ItemHolder {
        TextView txt_simple_money;
        TextView txt_simple_num;
        ImageView img_cb_simple_img_good;
        RelativeLayout rl_simple;
        LinearLayout layout_simple_message;
    }
}
